<template>
  <div>
    <el-row class="btn">
      <!-- <el-tag effect="dark" @click="queryList()">查询</el-tag> -->
      <el-tag effect="dark" @click="handleAdd">添加</el-tag>
    </el-row>
    <el-table :data="tableData" border class="table" :height="h" size="small" highlight-current-row>
      <el-table-column prop="name" class="t-title" resizable width="100" label="项目名称"></el-table-column>
      <el-table-column prop="date" class="t-title" resizable width="140" label="日期"></el-table-column>
      <el-table-column prop="desc" class="t-title" show-overflow-tooltip label="备注"></el-table-column>
      <el-table-column label="操作" class="t-title" width="140">
        <template slot-scope="scope">
          <el-button size="mini" icon="el-icon-edit" type="success" @click="handleEdit(scope.$index, scope.row)"></el-button>
          <el-button size="mini" icon="el-icon-delete" type="danger" @click="handleDelete(scope.$index, scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加表单 -->
    <el-dialog title="项目信息" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="desc">
          <el-input type="textarea" v-model="ruleForm.desc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
const pdb = require('../../lowdb/project')
export default {
  name: 'projects',
  data () {
    return {
      formState: 'add',
      tableData: [],
      dialogFormVisible: false,
      formLabelWidth: '120px',
      h: window.innerHeight - 120 + 'px',
      ruleForm: {
        id: '',
        name: '',
        date: '',
        desc: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入项目名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
        ],
        desc: [{ required: false, message: '请填写备注', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.queryList()
  },
  methods: {
    // 查询列表
    queryList () {
      let list = pdb.queryList()
      let td = []
      list.forEach(element => {
        td.push({id: element.id, name: element.name, date: element.date, desc: element.desc})
      })
      this.tableData = td
    },
    // 表单校验
    submitForm (formName) {
      let self = this
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (self.formState === 'add') {
            pdb.addItem(this.ruleForm)
          } else {
            pdb.updateItem(self.ruleForm)
          }
          this.$message({
            message: '保存成功',
            type: 'success'
          })
          self.queryList()
          self.dialogFormVisible = false
        } else {
          // console.log('error submit!!')
          return false
        }
      })
    },
    // 添加
    handleAdd () {
      this.ruleForm = {
        id: '',
        name: '',
        date: '',
        desc: ''
      }
      this.formState = 'add'
      this.dialogFormVisible = true
    },
    // 修改数据
    handleEdit (index, row) {
      this.ruleForm = row
      this.dialogFormVisible = true
      this.formState = 'edit'
    },
    // 删除数据
    handleDelete (index, row) {
      pdb.delItem(row)
      this.$message({
        message: '删除成功',
        type: 'success'
      })
      this.queryList()
    }
  }
}
</script>

<style scoped>
.btn {
  display: flex;
  flex-direction: row-reverse;
  align-items: right;
  margin: 10px 10px 10px 0;
}
.btn span {
  margin-left: 10px;
  cursor: pointer;
}
table {
  width: 100%;
  overflow-x: auto;
}
.t-title {
  text-align: center;
}
</style>
